package com.aitou.mrylive.presentation.ui.main.find;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.aitou.mrylive.R;
import com.aitou.mrylive.domain.LocalDataManager;
import com.aitou.mrylive.presentation.ui.base.BaseFragment;
import com.aitou.mrylive.presentation.ui.main.find.pengyouquan.MeiLiBangBean;
import com.aitou.mrylive.util.Const;
import com.aitou.mrylive.util.XImageLoader;

import java.util.ArrayList;
import java.util.List;

/**
 * 魅力榜Fragment
 * Created by admin on 2016/9/20.
 */
public class MeiLiBangFragment extends BaseFragment implements MeiLiBangInterFace, XListView.IXListViewListener {
    PaiHangBangPresenter pengyouqunpresenter;
    XListView billbord_recyclerview;
    MeiLiBangAdapter adapter;
    TextView content1, content2, content3, txt_name, txt_name3, txt_name2;
    ImageView tag_1, tag_2, tag_3, tag_4, tag_5, tag_6, img_big_1, img_big_2, img_big_3;
    ArrayList<MeiLiBangBean> arrayListCopy = new ArrayList<>();
    RelativeLayout num_1, num_2, num_3;
    int type=0;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_charm_billboard;
    }

    @Override
    protected void initViews(View view) {
        Bundle bundle=getArguments();
        type=bundle.getInt("type");
        View headerView = LayoutInflater.from(getActivity()).inflate(R.layout.header_meilipaihangbang, null);
        billbord_recyclerview = $(view, R.id.billbord_recyclerview);
        billbord_recyclerview.setXListViewListener(this);
        tag_1 = (ImageView) headerView.findViewById(R.id.tag_1);
        tag_2 = (ImageView) headerView.findViewById(R.id.tag_2);
        tag_3 = (ImageView) headerView.findViewById(R.id.tag_3);
        tag_4 = (ImageView) headerView.findViewById(R.id.tag_4);
        tag_5 = (ImageView) headerView.findViewById(R.id.tag_5);
        tag_6 = (ImageView) headerView.findViewById(R.id.tag_6);
        img_big_1 = (ImageView) headerView.findViewById(R.id.img_big_1);
        img_big_2 = (ImageView) headerView.findViewById(R.id.img_big_2);
        img_big_3 = (ImageView) headerView.findViewById(R.id.img_big_3);
        content1 = (TextView) headerView.findViewById(R.id.content1);
        content2 = (TextView) headerView.findViewById(R.id.content2);
        content3 = (TextView) headerView.findViewById(R.id.content3);
        txt_name = (TextView) headerView.findViewById(R.id.txt_name);
        txt_name3 = (TextView) headerView.findViewById(R.id.txt_name3);
        txt_name2 = (TextView) headerView.findViewById(R.id.txt_name2);
        num_1 = (RelativeLayout) headerView.findViewById(R.id.num_1);
        num_2 = (RelativeLayout) headerView.findViewById(R.id.num_2);
        num_3 = (RelativeLayout) headerView.findViewById(R.id.num_3);
        billbord_recyclerview.addHeaderView(headerView);
        pengyouqunpresenter = new PaiHangBangPresenter(this);

            System.out.println("type"+type+ LocalDataManager.getInstance().getLoginInfo().getToken() + "token" + LocalDataManager.getInstance().getLoginInfo().getCity());
            pengyouqunpresenter.getFuHaoBang(type);

    }


    @Override
    public void showData(List list) {

    }

    @Override
    public void appendData(List list) {

    }

    @Override
    public void showResult(List<MeiLiBangBean> list) {
        if (list != null) {
            if (adapter == null) {
                if (list.size() == 0) {
                    num_1.setVisibility(View.GONE);
                    num_2.setVisibility(View.GONE);
                    num_3.setVisibility(View.GONE);
                    billbord_recyclerview.setVisibility(View.GONE);
                }
                if (list.size() == 1) {
                    num_1.setVisibility(View.VISIBLE);
                    num_2.setVisibility(View.GONE);
                    num_3.setVisibility(View.GONE);
                } else if (list.size() == 2) {
                    num_1.setVisibility(View.VISIBLE);
                    num_2.setVisibility(View.VISIBLE);
                    num_3.setVisibility(View.GONE);
                } else if (list.size() == 3) {
                    num_1.setVisibility(View.VISIBLE);
                    num_2.setVisibility(View.VISIBLE);
                    num_3.setVisibility(View.VISIBLE);
                }
///////////////////////////////////////第一名
                if (list.size() >= 1) {
                    //名称
                    txt_name.setText(list.get(0).getNickname());
                    //头像
                    list.get(0).getAvatar();
                    //等级
                    if(type==0) {
                        tag_2.setBackgroundResource(R.drawable.rank_ + Integer.parseInt(list.get(0).getEmceelevel().get(0).getLevelid()));
                    }else{
                        tag_2.setBackgroundResource(R.drawable.rank_ + Integer.parseInt(list.get(0).getRichlevel().get(0).getLevelid()));
                    }
                    if (list.get(0).getSex() != null) {
                        if (list.get(0).getSex().equals("0")) {
                            tag_1.setBackgroundResource(R.drawable.ic_female);
                        } else {

                            tag_1.setBackgroundResource(R.drawable.ic_male);
                        }
                    }//内容
                    content1.setText(list.get(0).getAvatartime());
                    XImageLoader.load(Const.IMAG_NEW_URL + list.get(0).getSnap() + ".jpg", img_big_1);
                }


///////////////////////////////////////第二名
                if (list.size() >= 2) {
                    txt_name2.setText(list.get(1).getNickname());
                    list.get(1).getAvatar();
                    if(type==0) {
                        tag_4.setBackgroundResource(R.drawable.rank_ + Integer.parseInt(list.get(1).getEmceelevel().get(0).getLevelid()));
                    }else{
                        tag_4.setBackgroundResource(R.drawable.rank_ + Integer.parseInt(list.get(1).getRichlevel().get(0).getLevelid()));
                    }
                    if (list.get(1).getSex() != null) {
                        if (list.get(1).getSex().equals("0")) {
                            tag_3.setBackgroundResource(R.drawable.ic_female);
                        } else {

                            tag_3.setBackgroundResource(R.drawable.ic_male);
                        }
                    }
                    content2.setText(list.get(1).getAvatartime());
                    XImageLoader.load(Const.IMAG_NEW_URL + list.get(1).getSnap() + ".jpg", img_big_2);
                }


///////////////////////////////////////第二名
                if (list.size() >= 3) {
                    txt_name3.setText(list.get(2).getNickname());
                    list.get(2).getAvatar();

                    XImageLoader.load(Const.IMAG_NEW_URL + list.get(2).getSnap() + ".jpg", img_big_3);
                    if(type==0) {
                        tag_6.setBackgroundResource(R.drawable.rank_ + Integer.parseInt(list.get(2).getEmceelevel().get(0).getLevelid()));
                    }else{
                        tag_6.setBackgroundResource(R.drawable.rank_ + Integer.parseInt(list.get(2).getRichlevel().get(0).getLevelid()));
                    }
                    if (list.get(2).getSex() != null) {
                        if (list.get(2).getSex().equals("0")) {
                            tag_5.setBackgroundResource(R.drawable.ic_female);
                        } else {

                            tag_5.setBackgroundResource(R.drawable.ic_male);
                        }
                    }
                    content3.setText(list.get(2).getAvatartime());
                }
//                if(list.size()>=4) {
//                    list.addAll(list);
//                    list.addAll(list);
//                    list.addAll(list);
//                    list.addAll(list);
//                    list.addAll(list);
//
//                if (list.size() >= 4) {
//                    int count = 0;
//                    Iterator iterator = list.iterator();
//                    while (iterator.hasNext()) {
//                        iterator.remove();
//                        count++;
//                        if (count >= 3) {
//                            break;
//                        }
//                    }
//                }
                if (list.size() >= 4) {
                    int count = 0;
                    for (MeiLiBangBean meiLiBangBean : list) {
                        if (count >= 3) {
                            arrayListCopy.add(meiLiBangBean);
 }
                        count++;
                    }
                }
                adapter = new MeiLiBangAdapter(arrayListCopy);
                billbord_recyclerview.setAdapter(adapter);
//                }
            }
//            else {
//                adapter.notifyDataSetChanged();
//            }
        }
    }

    @Override
    public void onRefresh() {

        stopLoad();
    }

    @Override
    public void onLoadMore() {
        stopLoad();
    }

    public void stopLoad(){
        billbord_recyclerview.stopLoadMore();
        billbord_recyclerview.stopRefresh();
    }

    public class MeiLiBangAdapter extends BaseAdapter {
        List<MeiLiBangBean> list;

        public MeiLiBangAdapter(List<MeiLiBangBean> list) {
            this.list = list;
        }

        @Override
        public int getCount() {
            return list == null ? 0 : list.size();
        }

        @Override
        public Object getItem(int position) {
            return list == null ? 0 : list.get(position);
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            Holder holder = null;
            if (convertView == null) {
                convertView = LayoutInflater.from(getActivity()).inflate(R.layout.item_billboard, null);
                holder = new Holder();
                holder.item_billboard_tag1 = (ImageView) convertView.findViewById(R.id.item_billboard_tag1);
                holder.item_billboard_tag2 = (ImageView) convertView.findViewById(R.id.item_billboard_tag2);
                holder.txt_number = (TextView) convertView.findViewById(R.id.txt_number);
                holder.item_billboard_name = (TextView) convertView.findViewById(R.id.item_billboard_name);
                holder.item_billboard_contant = (TextView) convertView.findViewById(R.id.item_billboard_contant);
                holder.my_me_photo = (RoundImageView) convertView.findViewById(R.id.my_me_photo);
                convertView.setTag(holder);
            } else {
                holder = (Holder) convertView.getTag();
            }
            holder.txt_number.setText((4 + position) + "");
            if (list.get(position).getSex() != null) {
                if (list.get(position).getSex().equals("0")) {
                    holder.item_billboard_tag1.setBackgroundResource(R.drawable.ic_female);
                } else {
                    holder.item_billboard_tag1.setBackgroundResource(R.drawable.ic_male);
                }
            }

            holder.item_billboard_name.setText(this.list.get(position).getNickname());
            holder.item_billboard_contant.setText(this.list.get(position).getAvatartime());
            holder.item_billboard_tag2.setBackgroundResource(R.drawable.rank_ + Integer.parseInt(list.get(position).getEmceelevel().get(0).getLevelid()));
            XImageLoader.load(Const.IMAG_NEW_URL + list.get(position).getAvatar() + ".jpg", holder.my_me_photo);
            return convertView;
        }

        class Holder {
            TextView txt_number, item_billboard_name, item_billboard_contant;
            ImageView item_billboard_tag1, item_billboard_tag2;
            RoundImageView my_me_photo;
        }
    }
}
